package cn.maxwell.org.service.impl;

import cn.maxwell.basic.service.impl.BaseServiceImpl;
import cn.maxwell.basic.util.BaiduAuditUtils;
import cn.maxwell.basic.util.BusinessException;
import cn.maxwell.basic.util.Md5Utils;
import cn.maxwell.basic.util.StrUtils;
import cn.maxwell.org.domain.Employee;
import cn.maxwell.org.domain.Shop;
import cn.maxwell.org.dto.ShopDto;
import cn.maxwell.org.mapper.EmployeeMapper;
import cn.maxwell.org.mapper.ShopMapper;
import cn.maxwell.org.service.IShopService;
import cn.maxwell.user.domain.Logininfo;
import cn.maxwell.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 商家入驻逻辑步骤：
 * 一：检验
 * 1.1.每个参数的空值判断
 * 1.2.两次密码不一致校验
 * 1.3.该店铺是否已经被入驻过
 * * 二：入驻业务
 * * 2.1.保存店铺管理员信息t_employee
 * * 2.2.保存店铺信息t_shop
 * * 2.2.将店铺的id更新到t_employee中
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper empMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;

    /**
     * 店铺入驻
     * @param shop
     */
    @Transactional
    @Override
    public void settlement(Shop shop) {

        System.out.println(shop);
        //获取employee（相当于shop管理员的对象）
        Employee employee = shop.getEmployee();

        //1.检验输入的信息是否为空
        if (StringUtils.isEmpty(shop.getName())) {
            throw new BusinessException("店铺名不能为空");
        }
        if (StringUtils.isEmpty(shop.getAddress())) {
            throw new BusinessException("店铺地址不能为空");
        }
        if (StringUtils.isEmpty(shop.getTel())) {
            throw new BusinessException("店铺电话不能为空");
        }
        if (StringUtils.isEmpty(employee.getUsername())) {
            throw new BusinessException("管理员姓名不能为空");
        }
        //检验店铺人员信息
        if (StringUtils.isEmpty(employee.getEmail())) {
            throw new BusinessException("管理员邮箱不能为空");
        }
        if (StringUtils.isEmpty(employee.getPhone())) {
            throw new BusinessException("管理员电话不能为空");
        }
        if (StringUtils.isEmpty(employee.getPassword())) {
            throw new BusinessException("管理员密码不能为空");
        }
        if (StringUtils.isEmpty(employee.getComfirmPassword())) {
            throw new BusinessException("管理员的二次确认密码不能为空");
        }
        //2.判断两次输入的密码是否相同
        if (!shop.getEmployee().getPassword().equals(shop.getEmployee().getComfirmPassword())) {
            throw new BusinessException("二次确认密码输入错误");
        }

        //4.判断店铺名是否符合规范
        Boolean censor = BaiduAuditUtils.TextCensor(shop.getName());
        if(!censor){
            throw new BusinessException("店铺名称不符合规范，请重新输入！");
        }

        //3.判断店铺是否已经入驻过
        Shop shopTemp = shopMapper.loadByNameAndAddress(shop);
        if (shopTemp != null) {
            throw new BusinessException("店铺已经入驻，请直接登陆！忘记密码请找回密码");
        }




        //二：入驻业务

        //1.添加员工数据到数据库
        //1.1 设置盐值
        String salt = StrUtils.getComplexRandomString(32);
        employee.setSalt(salt);
        //1.2 设置加密加盐的密码
        String password = employee.getPassword();
        String md5Password = Md5Utils.encrypByMd5(password + salt);
        employee.setPassword(md5Password);
        //employee.setPassword(Md5Utils.encrypByMd5(employee.getPassword())+salt);
        //1.3 添加到数据库employee表
        empMapper.add(employee);

        //2.更新数据库shop表
        //2.1 获取employee店铺员工的自增主键赋值给shop表中的admin_id字段（管理员字段）
        shop.setAdmin_id(employee.getId());
        shop.setLogo(shop.getLogo());
        System.out.println(shop.getLogo());
        //2.2 保存shop数据
        shopMapper.add(shop);

        //3.更新数据库employee的shop_id字段
        //3.1 获取shop的自增主键赋值给employee的shop_id字段
        employee.setShop_id(shop.getId());
        //3.2 更新到数据库，更新employee表
        empMapper.update(employee);

        //4.保存logininfo登录信息
        Logininfo logininfo = logininfo2employee(employee);
        logininfoMapper.add(logininfo);

        //5.获取logininfo表的自增id赋值给employee表的logininfo_id字段
        employee.setLogininfo_id(logininfo.getId());
        //5.1 更新修改信息到employee
        empMapper.update(employee);

    }

    private Logininfo logininfo2employee(Employee employee) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        //设置type
        logininfo.setType(0);
        return logininfo;
    }


    /**
     * 批量添加
     * @param shop
     */
    @Transactional
    @Override
    public void patchInsert(List<Shop> shop) {
        shopMapper.patchInsert(shop);
    }


    /**
     * 店铺可视化图表查询方法
     */
    @Transactional
    @Override
    public List<ShopDto> getCountByState() {

        return shopMapper.getCountByState();
    }
}